﻿using Bentley.DgnPlatformNET;
using Bentley.DgnPlatformNET.Elements;
using Bentley.MstnPlatformNET;
using CheccCoverplateCulvertAssemble.Common;
using CulvertInitializationDatas;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;
using BIM = Bentley.Interop.MicroStationDGN;
using BMI = Bentley.MstnPlatformNET.InteropServices;

namespace CheccTexturePropertyGive
{
    public class SetCoverCulvertTexture
    {
        public enum CoverCulvertConstruction
        {
            //墙身
            WallBody,
            //基础
            Basis,
            //帽石
            CapStone,
            //端墙
            HeadWall,
            //沉降缝接头
            SettlingJoint,
            //换填
            Replacement,
            //端墙基础
            HeadWallBasis,
            //支撑梁
            SupportBeam,
            //正盖板
            PositiveSlab,
            //斜盖板
            CrookedSlab
        }
        private string LevelAndMatrialJsonFilePath = $@"{ConfigurationManager.GetVariable("Checc_CulvertCustomPath")}\Standards\涵洞\涵洞_属性图层材质设置_当前值.ini";
        public void CoverCulvertTexture(Element element, CoverCulvertConstruction culvertType)
        {
            Data tempSerializeDatas = JsonConvert.DeserializeObject<Data>(File.ReadAllText(LevelAndMatrialJsonFilePath));
            var materialName = "";
            var levelName = "";
            switch (culvertType)
            {
                case CoverCulvertConstruction.WallBody:
                    //盖板涵 墙身材质
                    materialName = tempSerializeDatas.SlabCulvertData.WallBody.Texture;
                    //盖板涵 墙身图层
                    levelName = tempSerializeDatas.SlabCulvertData.WallBody.Layer;
                    break;
                case CoverCulvertConstruction.Basis:
                    //盖板涵 基础材质
                    materialName = tempSerializeDatas.SlabCulvertData.Basis.Texture;
                    //盖板涵 基础图层
                    levelName = tempSerializeDatas.SlabCulvertData.Basis.Layer;
                    break;
                case CoverCulvertConstruction.CapStone:
                    //盖板涵 帽石材质
                    materialName = tempSerializeDatas.SlabCulvertData.CapRock.Texture;
                    //盖板涵 帽石图层
                    levelName = tempSerializeDatas.SlabCulvertData.CapRock.Layer;
                    break;
                case CoverCulvertConstruction.HeadWall:
                    //盖板涵 端墙材质
                    materialName = tempSerializeDatas.SlabCulvertData.Headwall.Texture;
                    //盖板涵 端墙图层
                    levelName = tempSerializeDatas.SlabCulvertData.Headwall.Layer;
                    break;
                case CoverCulvertConstruction.SettlingJoint:
                    //盖板涵 沉降缝接头材质
                    materialName = "";
                    //盖板涵 沉降缝接头图层
                    levelName = "盖板涵_沉降缝接头";
                    break;
                case CoverCulvertConstruction.Replacement:
                    //盖板涵 换填材质
                    materialName = tempSerializeDatas.SlabCulvertData.Replacement.Texture;
                    //盖板涵 换填图层
                    levelName = tempSerializeDatas.SlabCulvertData.Replacement.Layer;
                    break;
                case CoverCulvertConstruction.HeadWallBasis:
                    //盖板涵 端墙基础材质
                    materialName = tempSerializeDatas.SlabCulvertData.Headwall.Texture;
                    //盖板涵 端墙基础图层
                    levelName = tempSerializeDatas.SlabCulvertData.Headwall.Layer;
                    break;
                case CoverCulvertConstruction.SupportBeam:
                    //盖板涵 支撑梁材质
                    materialName = tempSerializeDatas.SlabCulvertData.BraceSummer.Texture;
                    //盖板涵 支撑梁图层
                    levelName = tempSerializeDatas.SlabCulvertData.BraceSummer.Layer;
                    break;
                case CoverCulvertConstruction.PositiveSlab:
                    //正盖板材质
                    materialName = tempSerializeDatas.SlabCulvertData.PositiveSlab.Texture;
                    //正盖板图层
                    levelName = tempSerializeDatas.SlabCulvertData.PositiveSlab.Layer;
                    break;
                case CoverCulvertConstruction.CrookedSlab:
                    //斜盖板材质
                    materialName = tempSerializeDatas.SlabCulvertData.CrookedSlab.Texture;
                    //斜盖板图层
                    levelName = tempSerializeDatas.SlabCulvertData.CrookedSlab.Layer;
                    break;
                default:
                    break;
            }
            //赋予图层
            AssignLevelToElement(element, levelName);
            //赋予材质
            AssignMaterialToElement(element, materialName);
        }

        //赋予材质
        public void AssignMaterialToElement(Element element, string materialName)
        {
            //if (materialName == null) return;

            //MaterialId id = new MaterialId(materialName);//查找名为Pavers BC2的材料
            //MaterialPropertiesExtension propertiesExtension = MaterialPropertiesExtension.GetAsMaterialPropertiesExtension(element as DisplayableElement);//为拉伸实体元素设置材料属性 
            //propertiesExtension.AddMaterialAttachment(id);//添加嵌入的材料信息
            //propertiesExtension.StoresAttachmentInfo(id);//保存拉伸实体元素的材料信息 
            //propertiesExtension.ReplaceInModel(element);//替换实体
            //Session.Instance.GetActiveDgnFile().SetFullSaveFlag();
            if (materialName == null) return;
            DgnDocumentMoniker dgnDocumentMoniker1 = Session.Instance.GetActiveDgnFile().GetDocument().GetMoniker(); 
            MaterialManager.LoadPaletteTable(Session.Instance.GetActiveDgnModel(), dgnDocumentMoniker1);

            var material = GetMaterialIdByName(materialName);
            if (material == null) return;
            var materialId = new MaterialId(material.Name);
            var displayableElement = element as DisplayableElement;
            var materialPropertiesExtension = displayableElement.GetAsMaterialPropertiesExtension();
            BentleyStatus bentleyStatus = materialPropertiesExtension.AddMaterialAttachment(materialId);
            BentleyStatus bentleyStatus2 = materialPropertiesExtension.AttachSubEntityMaterial(new string[] { "Face" }, materialId);
            materialPropertiesExtension.StoresAttachmentInfo(materialId);
            StatusInt ret = materialPropertiesExtension.ReplaceInModel(element);
        }
        private Material GetMaterialIdByName(string materialName)
        {
            var ss = MaterialManager.GetPalettesInSearchPath("MS_MATERIAL").ToList();
            PaletteInfo paletteInfo = MaterialManager.GetPalettesInSearchPath("MS_MATERIAL").ToList().FirstOrDefault(p => p.Name == "CHECC_Materials");
            var materials = MaterialManager.FindMaterialsInPalette(out MaterialSearchStatus status, paletteInfo, Session.Instance.GetActiveDgnModel(), true).ToList();
            var material = materials.FirstOrDefault(m => m.Name == materialName);
            return material;
        }
        //赋予图层
        private void AssignLevelToElement(Element element, string levelName)
        {
            var elementPropertiesSetter = new ElementPropertiesSetter();
            var levelId = CreateLevelByName(levelName);
            elementPropertiesSetter.SetLevel((LevelId)levelId);
            elementPropertiesSetter.Apply(element);
        }
        private LevelId? CreateLevelByName(string levelName)
        {
            FileLevelCache fileLevelCache = Session.Instance.GetActiveDgnFile().GetLevelCache();
            fileLevelCache.Write();
            LevelHandle levelHandle = fileLevelCache.GetLevelByName(levelName);
            if (!levelHandle.IsValid)
            {
                EditLevelHandle editLevelHandle = fileLevelCache.CreateLevel(levelName);
                fileLevelCache.Write();
                return editLevelHandle.LevelId;
            }
            return levelHandle.LevelId;
        }

    }
}
